/**
 * Created by gongxun on 17/1/18.
 */

var PszTablePlayer = GameBaseItem.extend({
    mSeat : null,
    Image_head : null,
    Text_nick : null,
    Text_score : null,
    Image_ready : null,
    Image_host : null,
    Image_offline : null,

    Panel_playing : null,
    Panel_move : null,
    ctor: function (seat) {
        this._super();
        this.loadCocostudio("res/ccs/PszTablePlayer.json");
        mo.gameHelper.fixIosText(this.ccsNode);
        this.mSeat = seat;

        //基础
        this.Image_head = mo.seekWidgetByName(this.ccsNode, "Image_head");
        this.Text_nick = mo.seekWidgetByName(this.ccsNode, "Text_nick");
        this.Text_score = mo.seekWidgetByName(this.ccsNode, "Text_score");
        this.Image_ready = mo.seekWidgetByName(this.ccsNode, "Image_ready");
        this.Image_host = mo.seekWidgetByName(this.ccsNode, "Image_host");
        this.Image_offline = mo.seekWidgetByName(this.ccsNode, "Image_offline");

        this.Image_ready.setVisible(false);
        this.Image_host.setVisible(false);
        this.Image_offline.setVisible(false);

        //打牌相关
        this.loadPlaying();

        //按钮
        mo.seekWidgetByName(this.ccsNode, "Image_player").addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEnd(type)) {
                if (mo.pszTable.mPlayerType == 0){
                    return;
                }

                var index = seat;
                var logicSeat = mo.pszTableStatus.getLogicForView(index, mo.pszTableStatus.mTableStatus.MySeat - 1) + 1;
                var seatUser = mo.pszTableStatus.mTableStatus["SeatUser" + logicSeat];
                if (seatUser != null){
                    mo.pszTable.mPszTableUILayer.mPszTablePlayerInfoLayer.showPlayerInfo(index, seatUser.user_info, mo.pszTable.mPszTableUILayer.mPlayerPoss[index], logicSeat - 1);
                }
            }
        }, this);

        //打牌中的
        this.Panel_playing.setVisible(false);
    },

    setPlayerInfo : function(info){
        if (info != null){
            this.setVisible(true);

            //昵称
            mo.gameHelper.setTextString(this.Text_nick, mo.gameHelper.getUserNickFilter(info.user_info.nickname));

            //头像
            mo.gameHelper.loadImageForUrl(this.Image_head, info.user_info.faceUrl);

            //分数
            this.Text_score.stopAllActions();
            if (mo.pszTableStatus.mTableStatus.Step == 0){
                if (this.Text_score.getString() == "-999"){
                    mo.gameHelper.setTextString(this.Text_score, info.RTScore + "");
                }else{
                    this.Text_score.runAction(cc.sequence(
                        cc.delayTime(2),
                        cc.callFunc(function(){
                            mo.gameHelper.setTextString(this, info.RTScore + "");
                        }, this.Text_score)
                    ));
                }
            }else{
                mo.gameHelper.setTextString(this.Text_score, info.RTScore + "");
            }

            //准备状态
            if (mo.pszTableStatus.mTableStatus.Step == 0 && info.State == 1){
                if (this.mSeat == 0){
                    this.Image_ready.setVisible(false);
                    this.reset();

                    var isPlay = mo.pszTableStatus.mTableStatus.LeftPlayTimes != mo.pszTable.mGameConfig.maxpan;
                    if (isPlay){
                        this.Image_ready.setVisible(true);
                    }
                }else{
                    this.Image_ready.setVisible(true);
                }
            }else{
                this.Image_ready.setVisible(false);
            }

            //庄家
            if (info.IsHost){
                this.Image_host.setVisible(true);
            }else{
                this.Image_host.setVisible(false);
            }
        }else {
            this.setVisible(false);
        }
    },

    setOnlineStatus : function(isOffline){
        this.Image_offline.setVisible(isOffline);
    },

    loadPlaying : function(){
        this.Panel_playing = mo.seekWidgetByName(this.ccsNode, "Panel_playing");

        if (this.mSeat == 0){
            this.Panel_move = mo.seekWidgetByName(this.ccsNode, "Panel_move0");
            mo.seekWidgetByName(this.ccsNode, "Panel_move1").removeFromParent();
            mo.seekWidgetByName(this.ccsNode, "Panel_move2").removeFromParent();
            // this.Panel_playing.removeChildByName("Panel_move1");
            // this.Panel_playing.removeChildByName("Panel_move2");
        }else if (this.mSeat == 3 || this.mSeat == 4){
            this.Panel_move = mo.seekWidgetByName(this.ccsNode, "Panel_move1");
            // this.Panel_playing.removeChildByName("Panel_move0");
            // this.Panel_playing.removeChildByName("Panel_move2");
            mo.seekWidgetByName(this.ccsNode, "Panel_move0").removeFromParent();
            mo.seekWidgetByName(this.ccsNode, "Panel_move2").removeFromParent();
        }else if (this.mSeat == 1 || this.mSeat == 2){
            this.Panel_move = mo.seekWidgetByName(this.ccsNode, "Panel_move2");
            // this.Panel_playing.removeChildByName("Panel_move0");
            // this.Panel_playing.removeChildByName("Panel_move1");
            mo.seekWidgetByName(this.ccsNode, "Panel_move0").removeFromParent();
            mo.seekWidgetByName(this.ccsNode, "Panel_move1").removeFromParent();
        }
    },

    getCardsPanelP : function(scale){
        var p = mo.seekWidgetByName(this.Panel_move, "Panel_cards").getPosition();
        p = cc.pMult(p, scale);
        return p;
    },

    //设置筹码
    setChip : function(num){
        var Text_chip = mo.seekWidgetByName(this.Panel_move, "Text_chip");
        mo.gameHelper.setTextString(Text_chip, num + "");
    },

    reset : function(){
        this.Panel_playing.setVisible(true);
        mo.seekWidgetByName(this.Panel_move, "Image_coins").setVisible(false);
        mo.seekWidgetByName(this.Panel_move, "Image_cardType").setVisible(false);
        mo.seekWidgetByName(this.Panel_move, "Image_bipai").setVisible(false);
    },

    //发牌
    fapai : function(){
        mo.seekWidgetByName(this.Panel_move, "Image_coins").stopAllActions();
        mo.seekWidgetByName(this.Panel_move, "Image_coins").runAction(cc.sequence(
            cc.delayTime(1.5),
            cc.show()
        ));
    },

    //显示牌型
    setCardType : function(cardType){
        mo.seekWidgetByName(this.Panel_move, "Image_cardType").setVisible(true);
        mo.gameHelper.loadTextureImage(mo.seekWidgetByName(this.Panel_move, "Image_cardType"), "res/ccs/psz/playerInfo/type_font" + cardType + ".png");
    },

    //显示手牌状态 0不显示 1已看牌 2比牌输了 3弃牌
    setInhandStatus : function(status, delayTime){
        var Image_bipai = mo.seekWidgetByName(this.Panel_move, "Image_bipai");
        Image_bipai.stopAllActions();
        if (status == 0){
            Image_bipai.setVisible(false);
        }else{
            var time = 0;
            if (delayTime != undefined){
                time = delayTime;
            }
            Image_bipai.runAction(cc.sequence(
                cc.delayTime(time),
                cc.show()
            ))

            var Text_bipai = mo.seekWidgetByName(Image_bipai, "Text_bipai");
            var strText = "";
            if (status == 1){
                strText = "已看牌";
            }else if (status == 2){
                strText = "比牌输";
            }else if (status == 3){
                strText = "弃牌";
            }
            mo.gameHelper.setTextString(Text_bipai, strText);
        }
    }
});